package com.jt.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jt.mapper.ItemCatMapper;
import com.jt.pojo.ItemCat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ItemCatServiceImpl implements ItemCatService {

    @Autowired
    private ItemCatMapper itemCatMapper;

    /**
     * 问题分析:
     *  1.提高程序效率, 减少数据库交互的次数
     *  2.查询的方法最好单独的抽取
     *
     *  数据结构:
     *
     *  type=1/2/3
     *
     * */

    //1.封装一个Map集合
    public Map<Integer,List<ItemCat>> getMap(){
        Map<Integer, List<ItemCat>> map = new HashMap<>();
        //1,查询所有数据
        List<ItemCat> list = itemCatMapper.selectList(null);
        //2.将List集合封装到Map集合中
        for (ItemCat itemCat : list) {
            //规则:判断map中是否有key
            if (map.containsKey(itemCat.getParentId())) {
                //有父级 获取父级的所有已知子集
               map.get(itemCat.getParentId()).add(itemCat);
            }else{
                //没有父级
                ArrayList<ItemCat> initList = new ArrayList<>();
                initList.add(itemCat);
                map.put(itemCat.getParentId(),initList);
            }
            }
        return map;
        }

    @Override
    public List<ItemCat> findItemCatList(Integer type) {
        Map<Integer, List<ItemCat>> map = getMap();
        //ArrayList<ItemCat> catList = new ArrayList<>();
        if (type == 1){
            return map.get(0);
        }
        if (type == 2){
            return getLevel2(map);
        }
        return getLevel3(map);
    }

    @Override
    @Transactional //事务控制
    public void updateStatus(ItemCat itemCat) {
        itemCatMapper.updateById(itemCat);
    }

    @Override
    @Transactional
    public void saveItemCat(ItemCat itemCat) {
        itemCat.setStatus(true);
        itemCatMapper.insert(itemCat);
    }

    @Override
    @Transactional
    public void deleteItemCat(Integer id, Integer level) {
        if (level == 3){
            //如果是3级商品分类的菜单则直接删除
            itemCatMapper.deleteById(id);
        }
        if (level == 2){
            //需要删除的是2级菜单,关联删除3级菜单
            //先删除3级菜单
            QueryWrapper<ItemCat> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("parent_id", id);
            itemCatMapper.delete(queryWrapper);
            //再删除2级菜单
            itemCatMapper.deleteById(id);
        }
        if (level == 1){
            //1.查询二级分类信息
            QueryWrapper<ItemCat> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("parent_id", id);
            //selectObjs获取主键信息 (第一列信息)
            List<Object> twoIdlist = itemCatMapper.selectObjs(queryWrapper);
            //先删除3级
            for (Object twoId : twoIdlist) {
                QueryWrapper<ItemCat> queryWrapper2 = new QueryWrapper<>();
                queryWrapper2.eq("parent_id", twoId);
                itemCatMapper.delete(queryWrapper2);
                //再删除2级
                Integer intTwoId = (Integer) twoId;
                itemCatMapper.deleteById(intTwoId);
            }
            //最后删除1级
            itemCatMapper.deleteById(id);
        }
    }

    private List<ItemCat> getLevel3(Map<Integer, List<ItemCat>> map) {
        List<ItemCat> list = getLevel2(map);
        for (ItemCat itemCat1 : list) {
            List<ItemCat> list2 = itemCat1.getChildren();
            if (list2 == null) continue;
            for (ItemCat itemCat2 : list2) {
                List<ItemCat> list3 = map.get(itemCat2.getId());
                itemCat2.setChildren(list3);
            }
            itemCat1.setChildren(list2);
        }
        return list;
    }

    private List<ItemCat> getLevel2(Map<Integer, List<ItemCat>> map) {
        //1.获取1级商品分类信息
        List<ItemCat> list = map.get(0);

        //2.封装2级菜单信息
        for (ItemCat itemCat : list) {
            //如何获取一级菜单的子集
            List<ItemCat> twoList = map.get(itemCat.getId());
            itemCat.setChildren(twoList);
        }
        return list;
    }




   /* @Override
    public List<ItemCat> findItemCatList(Integer type) {
        QueryWrapper<ItemCat> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("parent_id", 0);
        List<ItemCat> list = itemCatMapper.selectList(queryWrapper1);
        for (ItemCat itemCat : list) {
            QueryWrapper queryWrapper2 = new QueryWrapper();
            queryWrapper2.eq("parent_id", itemCat.getId());
            List<ItemCat> list1 = itemCatMapper.selectList(queryWrapper2);
            for (ItemCat itemCat1 : list1) {
               QueryWrapper queryWrapper3= new QueryWrapper();
               queryWrapper3.eq("parent_id", itemCat1.getId());
                List list2 = itemCatMapper.selectList(queryWrapper3);
                itemCat1.setChildren(list2);
            }
            itemCat.setChildren(list1);
        }
        return list;
    }*/
}
